package ar.edu.unicen.pladema.vc1.practico;

import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

import ar.edu.unicen.pladema.main.SWTUtils;
import ar.edu.unicen.pladema.ui.mywidgets.ElapsedPanel;
import ar.edu.unicen.pladema.ui.mywidgets.SizePanel;
import ar.edu.unicen.pladema.ui.panel.AbstractPanel;
import ar.edu.unicen.pladema.ui.panel.options.IPaintIt;
import ar.edu.unicen.pladema.vc1.model.GraphicAttributes;

public abstract class PanelOption extends AbstractPanel implements Observer {
	private Label opciones;
	private Button recargar;
	private SizePanel sizePanel;
	private ElapsedPanel elapsedPanel;
	protected IPaintIt ipi;
	protected GraphicAttributes graphicAttributes;
	
	public PanelOption(Composite parent, IPaintIt pi, GraphicAttributes ga) {
		super(parent);
		this.ipi = pi;
		this.graphicAttributes = ga;
		ga.addObserver(this);
	}

	@Override
	protected final void createWidgets() {
		opciones = new Label(this, SWT.NONE);
		opciones.setText("OPCIONES");
		Font f = new Font(this.getDisplay(), "Arial", 24, SWT.BOLD);
		opciones.setFont(f);
		
		recargar = SWTUtils.createButton(this, "DRAW", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				saveGraphicData();
				ipi.doPaint();
			}
		});
		sizePanel = new SizePanel(this);
		elapsedPanel = new ElapsedPanel(this);
		createWidgetsImpl();
	}

	@Override
	protected final void layoutWidgets() {
		setLayout(new FormLayout());
		
		opciones.setLayoutData(SWTUtils.createTopFormData(5));
		sizePanel.setLayoutData(SWTUtils.createBelowFormData(opciones, 5));
		elapsedPanel.setLayoutData(SWTUtils.createBelowFormData(sizePanel, 5));
	
		recargar.setLayoutData(layoutWidgets(SWTUtils.createBelowFormData(elapsedPanel, 5)));
	}
	protected abstract void createWidgetsImpl();
	protected abstract FormData layoutWidgets(FormData fd);
	protected abstract void saveGraphicData();
	protected abstract void loadGraphicData();
	
	@Override
	public final void update(Observable o, Object arg) {
		if (graphicAttributes.equals(o)) {
			sizePanel.setValue(
					graphicAttributes.get(GraphicAttributes.SIZE_WIDTH), 
					graphicAttributes.get(GraphicAttributes.SIZE_HEIGHT));
			elapsedPanel.setValue(graphicAttributes.get(GraphicAttributes.ELAPSED));
			loadGraphicData();
		}
	}
}
